package com.zrc.module_find.activity.defrecycleview.demo1

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zrc.module_find.R
import java.util.ArrayList

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
abstract class CommonAdapter<T>(context: Context, private val data: List<T>, layoutId: Int) :
    RecyclerView.Adapter<CommonAdapter.CommonViewHolder>() {
    private val mLayoutInflater: LayoutInflater = LayoutInflater.from(context)
    private val mLayoutId: Int = layoutId
    private var mFixX = 0
    val moveViewList = ArrayList<View>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonViewHolder {
        val itemView = mLayoutInflater.inflate(mLayoutId, parent, false)
        val holder = CommonViewHolder(itemView)
        val moveLayout = holder.getView<LinearLayout>(R.id.ll_move)//获取可滑动的view布局
        moveLayout.scrollTo(mFixX, 0)
        moveViewList.add(moveLayout)
        return holder
    }

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
        bindData(holder, data[position])
    }

    override fun getItemCount() = data.size

    abstract fun bindData(holder: CommonViewHolder, data: T)

    fun setFixX(fixX: Int) {
        mFixX = fixX
    }

    class CommonViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val viewSparseArray: SparseArray<View?> = SparseArray()

        //根据 ID 来获取 View
        fun <T : View> getView(viewId: Int): T {
            //先从缓存中找，找打的话则直接返回  如果找不到则 findViewById ，再把结果存入缓存中
            var view = viewSparseArray[viewId]
            if (view == null) {
                view = itemView.findViewById(viewId)
                viewSparseArray.put(viewId, view)
            }
            return view as T
        }

        fun setText(viewId: Int, text: CharSequence?): CommonViewHolder {
            val tv = getView<TextView>(viewId)
            tv.text = text
            return this
        }

        fun setImageResource(viewId: Int, resourceId: Int): CommonViewHolder {
            val imageView = getView<ImageView>(viewId)
            imageView.setImageResource(resourceId)
            return this
        }

        fun setOnClickListener(l: View.OnClickListener): CommonViewHolder {
            itemView.setOnClickListener(l)
            return this
        }

        fun <T : View> setOnChildClickListener(
            viewId: Int,
            l: View.OnClickListener
        ): CommonViewHolder {
            val view = getView<T>(viewId)
            view.setOnClickListener(l)
            return this
        }

        fun setOnLongClickListener(l: View.OnLongClickListener): CommonViewHolder {
            itemView.setOnLongClickListener(l)
            return this
        }

        fun <T : View> setOnLongChildClickListener(
            viewId: Int,
            l: View.OnLongClickListener
        ): CommonViewHolder {
            val view = getView<T>(viewId)
            view.setOnLongClickListener(l)
            return this
        }
    }
}